package com.zoke.password.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.zoke.password.Conf;
import com.zoke.password.R;
import com.zoke.password.base.BaseActivity;
import com.zoke.password.dialog.AppLoading;
import com.zoke.password.model.Result;
import com.zoke.password.model.User;
import com.zoke.password.tools.UToast;

import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;

import io.nebulas.Constants;
import io.nebulas.api.SmartContracts;
import io.nebulas.model.GoodsModel;
import io.nebulas.utils.Util;

@ContentView(R.layout.activity_login)
public class LoginActivity extends BaseActivity {

    private String serialNumber = "";

    private boolean isLogin = false;
    private AppLoading loading;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loading = new AppLoading(this, false, R.string.loading);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        if (mImmersionBar != null)
            mImmersionBar.statusBarDarkFont(true)
                    .init();
    }

    @Event({R.id.btn_login})
    private void onViewClick(View view) {
        login();
    }

    public void login() {
        //登录的nas call
        isLogin = true;
        if (loading != null) loading.show();
        serialNumber = Util.getRandomCode(Constants.RANDOM_LENGTH);
        GoodsModel goods = new GoodsModel();
        goods.name = "登录-设置user信息";
        goods.desc = "设置登录的一些use信息";
        String functionName = "setUser";
        //password, preference
        String[] args = new String[]{"", "0"};
        SmartContracts.call(this, Constants.MAIN_NET, goods, functionName, Conf.SMART, Conf.VALUE, args, serialNumber);
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (isLogin) {
            nasQueryTransferStatus();
        }
    }

    //更具serialNumber 获取交易状态
    public void nasQueryTransferStatus() {
        isLogin = false;
        if (TextUtils.isEmpty(serialNumber)) {
            if (loading != null) loading.dismiss();
            return;
        }
        SmartContracts.queryTransferStatus(Constants.MAIN_NET, serialNumber, new SmartContracts.StatusCallback() {
            @Override
            public void onSuccess(final String response) {
                LogUtil.e("onSuccess :" + response);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (loading != null) loading.dismiss();
                        if (TextUtils.isEmpty(response)) {
                            UToast.show(LoginActivity.this, R.string.login_error);
                            return;
                        }
                        Result result = new Gson().fromJson(response, Result.class);
                        if (result == null) {
                            UToast.show(LoginActivity.this, R.string.login_error);
                            return;
                        }
                        if (result.code != 0) {
                            UToast.show(LoginActivity.this, LoginActivity.this.getString(R.string.login_error) + ":" + result.msg);
                            return;
                        }
                        Result.ResultData data = result.data;
                        User user = new User();
                        user.password = "";
                        user.preference = "0";
                        user.created = data.timestamp;
                        user.address = data.from;//记录登录用的地址
                        User.save(user);
                        UToast.show(LoginActivity.this, R.string.login_success);
                        finish();
                        PatternLockActivity.open(LoginActivity.this,1,true);
//                        open(LoginActivity.this, PatternLockActivity.class);
                    }
                });
            }

            @Override
            public void onFail(final String error) {
                LogUtil.e("onFail " + error);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (loading != null) loading.dismiss();
                        UToast.show(LoginActivity.this, LoginActivity.this.getString(R.string.login_error) + ":" + error.toString());
                    }
                });
            }
        });
    }

}
